package net.a6polo.a6poloweibo;

import java.io.IOException;

import net.a6polo.a6poloweibo.adapter.RepostListViewAdapter;
import net.a6polo.a6poloweibo.sina.domain.Reposts;
import net.a6polo.a6poloweibo.util.WeiboUtil;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView;
import net.a6polo.a6poloweibo.view.MyProgressDialog;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView.OnRefreshListener;
import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class RepostTab extends Fragment {

	private Handler handler = null;

	private DropDownToRefreshListView repostListView = null;

	private static String weiboId;

	private StatusesAPI statuesApi;

	private Reposts reposts;

	private int pageNo = 1;

	private int pageSize = 20;

	private long maxId = 0;

	private Exception exception;

	private RepostListViewAdapter repostListViewAdapter;

	private Dialog progressDialog;

	private View view;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		handler = new Handler();
		repostListView = (DropDownToRefreshListView) view.findViewById(R.id.repostListView);
		progressDialog = new MyProgressDialog(getActivity(), R.style.CustomProgressDialog);

		weiboId = getArguments().getString("weiboId");

		Service service = new Service(getActivity());
		Account sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		Oauth2AccessToken token = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());

		statuesApi = new StatusesAPI(token);

		if (null == reposts || reposts.getReposts().size() == 0) {
			progressDialog.show();
			reRefresh();
		}

		repostListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				reRefresh();
				repostListView.onRefreshComplete("松开可刷新");
			}
		});

		repostListView.setOnLoadMoreListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				more();
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_repost, container, false);
		return view;
	}

	private void reRefresh() {
		maxId = 0;
		statuesApi.repostTimeline(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo,
				WeiboAPI.AUTHOR_FILTER.ALL, new RepostRefreshRequestListener());
	}

	class RepostRefreshRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			progressDialog.dismiss();
			Gson gson = new Gson();
			Log.d("a6polo", "repost response json:" + json);
			reposts = gson.fromJson(json, Reposts.class);

			int maxIndex = reposts.getReposts().size();
			if (maxIndex > 0) {
				maxIndex = maxIndex - 1;
				maxId = reposts.getReposts().get(maxIndex).getId();
			}

			new Thread() {
				public void run() {
					handler.post(runnableRefreshUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			});

		}

	}

	Runnable runnableRefreshUi = new Runnable() {

		@Override
		public void run() {
			loadRepostData(reposts);
		}
	};

	private void loadRepostData(Reposts reposts) {
		repostListViewAdapter = new RepostListViewAdapter(reposts, getActivity());
		repostListView.setAdapter(repostListViewAdapter);
	}

	private void more() {
		statuesApi.repostTimeline(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo,
				WeiboAPI.AUTHOR_FILTER.ALL, new RepostMoreRequestListener());
	}

	class RepostMoreRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			Gson gson = new Gson();
			reposts = gson.fromJson(json, Reposts.class);

			int maxIndex = reposts.getReposts().size();
			if (maxIndex > 0) {
				maxIndex = maxIndex - 1;
			}
			maxId = reposts.getReposts().get(maxIndex).getId();

			new Thread() {
				public void run() {
					handler.post(runnableMoreUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			});

		}

	}

	Runnable runnableMoreUi = new Runnable() {
		@Override
		public void run() {
			moreRepostData(reposts);
		}

	};

	private void moreRepostData(Reposts reposts) {
		repostListViewAdapter.addItem(reposts);
		repostListViewAdapter.notifyDataSetChanged();
	}
}
